$(document).ready(function(){
	
	reload_list_user();

	$('#btn_simpan').click(function(e){
		e.preventDefault();
		var nip= $('#nip').val();
		var password=$('#password').val();
		var password2=$('#password2').val();

		var e = document.getElementById('kewenangan');
        var kewenangan = e.options[e.selectedIndex].value;

        if(nip==='' || password==='' || password2==='') {
        	$('#error').html("Semua Field Harus Diisi!").fadeIn(500).delay(2500).fadeOut(500);
        }else if(password!==password2){
        	$('#error').html("Konfirmasi Password Tidak Sama").fadeIn(500).delay(2500).fadeOut(500);
        }else {

        	$.post('controller/cont.ruh.php',{aksi_rekam_user:true,nip:nip,password:password,id_kewenangan:kewenangan},function(data){
        		if(data.msg){
        			$('#success').html('Data Berhasil Direkam').fadeIn(500).delay(2500).fadeOut(500);
        			clear_input();
        		}else{
        			$('#error').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
        		}

        	},'json');
        }
		return false;
	});

	$('#btn_simpan_ubah').click(function(e){
		e.preventDefault();
		var id_user=$('#id_user').val();
		var nip= $('#nip').val();
		var password=$('#password').val();
		var password2=$('#password2').val();

		var e = document.getElementById('kewenangan');
        var kewenangan = e.options[e.selectedIndex].value;

        if(nip==='' || password==='' || password2===''){
        	$('#error').html("NIP dan Password tidak boleh kosong").fadeIn(500).delay(2500).fadeOut(500);
        }else if(password!==password2){
        	$('#error').html("Konfirmasi Password Salah").fadeIn(500).delay(2500).fadeOut(500);
        }else {
			$.post('controller/cont.ruh.php',{aksi_simpan_ubah_user:true,id_user:id_user,nip:nip,
				password:password,id_kewenangan:kewenangan},function(data){
					if(data.msg){
						$('#success').html('Data Berhasil Diubah').fadeIn(500).delay(2500).fadeOut(500);
        				clear_input();
					}else {
						$('#error').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
					}
			},'json');
		}
		return false;
	});


});



function hapus(id_user){
	$.getJSON('controller/cont.ruh.php',{aksi_hapus_user:true,id_user:id_user}, function(data){
		if(data.msg){
			alert("User berhasil dihapus");
			reload_list_user();
		}else {
			$('#error').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
		}
	});
}

function reload_list_user(){
	$.getJSON('controller/cont.list.php',{list_user:true},function(data){
		$('#list_users tbody').html('');
		$.each(data,function(index,data){
			var aktif='';
			var statusnya='Tidak Aktif';

			if(data.status_aktif==1){
				aktif='disabled';
				statusnya='Aktif';
			}
			var nip='';
			if (!data.id_parent_user) {
				nip='-';
			}else{
				nip=data.id_parent_user;
			}

			var tab="<tr>";
			tab +="<td>"+data.nip+"</td>";
			tab +="<td>"+nip+"</td>";
			tab +="<td>"+data.id_kewenangan+"</td>";
			tab +="<td>"+statusnya+"</td>";
			tab +="<td align=\"center\"><button type=\"button\" onclick=\"hapus("+ data.id_user+")\" class=\"btn btn-default btn-lg\"><span class=\"glyphicon glyphicon-remove-circle\"></span></button></td>";
			tab +="<td align=\"center\"><a data-toggle=\"modal\" onclick=\"ubah("+data.id_user+")\" class=\"btn btn-default btn-lg\"><span class=\"glyphicon glyphicon-edit\"></span></a></td>";
			tab +="<td align=\"center\"><a data-toggle=\"modal\" onclick=\"aktivasi("+data.id_user+")\" class=\"btn btn-default btn-lg\" "+aktif+"><span class=\"glyphicon glyphicon-saved\"></span>Aktivasi</a></td>";
			tab +="</tr>";
			//alert(tab);
			$('#list_users tbody').append(tab);
		});
	});
}

function clear_input(){
	$('#judul_modal').html('Rekam Data');
	$('#btn_simpan_ubah').hide();
	$('#btn_simpan').show();
	document.getElementById('nip').value = "";
	document.getElementById('password').value = "";
	document.getElementById('password2').value = "";
}

function ubah(id_user){
	$('#myModal').modal('toggle');
	$('#judul_modal').html('Ubah Data');
	$('#btn_simpan_ubah').show();
	$('#btn_simpan').hide();

	$.getJSON('controller/cont.ruh.php',{aksi_get_user_ubah:true, id_user:id_user},function(data){
		document.getElementById('id_user').value = data.id_user;
		document.getElementById('nip').value = data.nip;
		document.getElementById('password').value = "";
		document.getElementById('password2').value = "";
		var ddl = document.getElementById('kewenangan');
		var opts = ddl.options.length;
		for (var i=0; i<opts; i++){
		    if (ddl.options[i].value == data.id_kewenangan){
		        ddl.options[i].selected = true;
		        break;
		    }
		}
		return false;
	});

}

function aktivasi(id_user){
	$.getJSON('controller/cont.ruh.php',{aksi_aktivasi_user:true,id_user:id_user}, function(data){
		if(data.msg){
			alert("User berhasil diaktivasi");
			reload_list_user();
		}else {
			$('#error').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
		}
	});
}